
package com.google.demo.Data.Controler;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.protocol.HTTP;

import android.util.Log;

import com.google.demo.Util.ErrorCode;
import com.google.demo.Util.json.JsonReader;
import com.google.demo.Util.json.JsonToken;
import com.google.demo.model.DataResult;
import com.google.demo.model.MethodParams;
import com.google.demo.model.UserInfo;

public class ParserDataHelper implements IServerDataParser {
	private static final String TAG = "ParserDataHelper";
	private volatile static ParserDataHelper uniqueInstance;

	public static ParserDataHelper getInstance() {
		if (uniqueInstance == null) {
			synchronized (DataHelper.class) {
				if (uniqueInstance == null) {
					uniqueInstance = new ParserDataHelper();
				}
			}
		}
		return uniqueInstance;
	}

	/**
	 * Parse json string
	 * 
	 * @param data
	 * @param method
	 * @return All parser method return object ApiServiceResult
	 */
	public DataResult parse(InputStream data, MethodParams methodparam) {
		if (data == null) {
			return null;
		}
		JsonReader reader;
		try {
			reader = new JsonReader(new InputStreamReader(data, HTTP.UTF_8));
			reader.setLenient(true);
			switch (methodparam.getMethod()) {
			case postQRCodeIn:
				return parsePostQRCodeIn(reader);
			case postQRCode:
				return parsePostQRCode(reader);
			case postQRCodeOut:
				return parsePostQRCodeOut(reader);
			case login:
				return parsePostLogin(reader);
			default:
				break;
			}
		}

		catch (UnsupportedEncodingException e) {
			Log.e(TAG, "parse" + e.toString());
		}

		return null;
	}

	@Override
	public DataResult parsePostQRCodeIn(JsonReader reader) {
		// TODO Auto-generated method stub
		return parsePostQRCode(reader);
	}

	@Override
	public DataResult parsePostQRCode(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if(name.equals("status")){
					String status=reader.nextString();
					if(!status.equals("Success"))
					{
						result.setErrorCode(ErrorCode.SERVER_ERROR);
						return result;
					}
					else
						result.setData(status);
				}
				else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parsePostQRCode" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
		}
		return result;
	}

	@Override
	public DataResult parsePostQRCodeOut(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if(name.equals("status")){
					String status=reader.nextString();
					if(!status.equals("Success"))
					{
						result.setErrorCode(ErrorCode.SERVER_ERROR);
						return result;
					}
					else
						result.setData(status);
				}
				else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parsePostQRCode" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
		}
		return result;
	}
	
	@Override
	public DataResult parsePostLogin(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		UserInfo userInfo=new UserInfo();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if(name.equals("status")){
					String status=reader.nextString();
					if(!status.equals("Success"))
						userInfo.setmIsLogin(false);
					else
						userInfo.setmIsLogin(true);
				} else if(name.equals("DEVICE_ID")){
					String devid=reader.nextString();
					userInfo.setmDeviceId(devid);
				}else if(name.equals("USER_NAME")){
					String username=reader.nextString();
					userInfo.setmUsername(username);
				}else if(name.equals("PASSWORD")){
					String password=reader.nextString();
					userInfo.setmPassword(password);
				}else if(name.equals("FIRST_NAME")){
					String fname=reader.nextString();
					userInfo.setmFname(fname);
				}else if(name.equals("LAST_NAME")){
					String lname=reader.nextString();
					userInfo.setmLname(lname);
				}else if(name.equals("GENDER")){
					String gender=reader.nextString();
					userInfo.setmGender(gender);
				}else if(name.equals("DATE_OF_BIRTH")){
					String dob=reader.nextString();
					userInfo.setmDOB(dob);
				}else if(name.equals("ABOUT")){
					String about=reader.nextString();
					userInfo.setmAbout(about);
				}
				else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parsePostLogin" + e.toString());
			
		}
		result.setData(userInfo);
		return result;
	}
}
